
//  AppDelegate.swift
//  WeiBo14
//
//  Created by HM on 16/3/2.
//  Copyright © 2016年 HM. All rights reserved.
//

import UIKit
import SVProgressHUD

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?


    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
        SVProgressHUD.setBackgroundColor(UIColor.purpleColor())
        
        // 测试默认表情读取
//        let emoticons = HMEmoticonTools.sharedTools.allEmoticons
//        print(emoticons)
        // 测试Int32的最大值
        // print(INT32_MAX)
        
        // 测试解档
//        let file = (NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true).last! as NSString).stringByAppendingPathComponent("account.archive")
//        print(file)
//        let result = NSKeyedUnarchiver.unarchiveObjectWithFile(file) as? HMUserAccount
//        print(result)
        
        // 1. 初始化 window 
        window = UIWindow(frame: UIScreen.mainScreen().bounds)
        // 2. 设置根控制器
        // 如果没有登录,直接显示 HMMainViewController
        // 如果登录:显示HMWelcomeViewController
        
        let userLogon = HMUserAccountViewModel.sharedAccountViewModel.userLogon
        window?.rootViewController = userLogon ? HMWelcomeViewController() : HMMainViewController()
        // 3. 成为主窗口并可见
        window?.makeKeyAndVisible()
        
        // 注册根控制器切换的通知
        // 如果采用这种方式添加通知监听的话,就需要保存该方法的返回值,在deinit方法里面去陈移除通知的时候传入
        
        /**
            倒数第1个参数:通知触发要执行的闭包
            倒数第2个参数:代表该闭包在哪个队列里面执行
        */
        self.observer = NSNotificationCenter.defaultCenter().addObserverForName(HMSwitchRootVCNotification, object: nil, queue: NSOperationQueue.mainQueue()) { (noti) -> Void in
            // 判断如果是 OAuthVC 发送的通知,那么就切换到欢迎页面
            if noti.object!.isKindOfClass(HMOAuthViewController.self) {
                self.window?.rootViewController = HMWelcomeViewController()
            }else{
                self.window?.rootViewController = HMMainViewController()
            }
        }
        return true
    }
    
    var observer: NSObjectProtocol?
    // 析构函数,与 OC 中的 dealloc 类似,当对象将要销毁的时候会调用这个方法
    // 一般在这个方法里面做: 移除通知,释放某对象的内存,移除观察者
    deinit{
        NSNotificationCenter.defaultCenter().removeObserver(observer!)
    }

    func applicationWillResignActive(application: UIApplication) {
        // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
        // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
    }

    func applicationDidEnterBackground(application: UIApplication) {
        // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
        // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
    }

    func applicationWillEnterForeground(application: UIApplication) {
        // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
    }

    func applicationDidBecomeActive(application: UIApplication) {
        // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
    }

    func applicationWillTerminate(application: UIApplication) {
        // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
    }


}

